package com.magus.project.yddemo.controller;

import static org.springframework.http.HttpStatus.NOT_FOUND;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.magus.framework.core.controller.BaseController;
import com.magus.framework.core.entity.BaseTreeNode.WithOutTreeView;
import com.magus.framework.system.annotation.AppFunctionInfo;
import com.magus.framework.system.annotation.AppFunctionInfo.FunctionType;
import com.magus.framework.system.annotation.AppGroupInfo;
import com.magus.project.yddemo.entity.IndicatorPackage;
import com.magus.project.yddemo.service.IndicatorPackageService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 指标包
 * 
 * @author Yingjian Wang
 * @date 2018-01-25 11:20
 */
@ApiIgnore
@RestController
@AppGroupInfo(key = "yddemo:indicatorPackage", name = "指标下发管理", appKey = "yddemo")
@RequestMapping("/yddemo/indicatorPackages")
public class IndicatorPackageController extends BaseController {
	@Autowired
	private IndicatorPackageService indicatorPackageService;
	
	/**
	 * 根据ID查询指标信息
	 * 
	 * @param indicatorId
	 *            指标ID
	 * @return 指标信息
	 */
	@GetMapping("/{packagrId}")
	@RequiresPermissions("yddemo:indicator:view")
	@ApiOperation(value = "查询指标", notes = "根据ID查询指标信息")
	ResponseEntity<IndicatorPackage> findOne(@PathVariable String packagrId) {

		IndicatorPackage indicatorPackage = indicatorPackageService.findOne(packagrId);
		if (indicatorPackage == null) {
			return ResponseEntity.status(NOT_FOUND).body(null);
		}

		return ResponseEntity.ok(indicatorPackage);
	}
	
	/**
	 * 新增指标组
	 * 
	 * @param group
	 * @param result
	 * @return
	 */
	@JsonView(WithOutTreeView.class)
	@PostMapping
	@RequiresPermissions("yddemo:indicatorPackage:add")
	@ApiOperation(value = "新增指标包", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:indicatorPackage:add", name = "新增指标组", groupKey = "yddemo:indicator", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorPackages/page/add")
	ResponseEntity<IndicatorPackage> add(@RequestBody IndicatorPackage group, BindingResult result) {
		validate(group, result);
		IndicatorPackage saved = indicatorPackageService.save(group);
		return ResponseEntity.status(HttpStatus.CREATED).body(saved);
	}

	/**
	 * 编辑指标组
	 * 
	 * @param id
	 * @param group
	 * @param result
	 * @return
	 */
	@JsonView(WithOutTreeView.class)
	@PutMapping("/{id}")
	@RequiresPermissions("yddemo:indicatorPackage:edit")
	@ApiOperation(value = "编辑指标包", notes = "返回成功保存后的对象")
	@AppFunctionInfo(key = "yddemo:indicatorPackage:edit", name = "编辑指标组", groupKey = "yddemo:indicatorPackage", //
			isMenu = false, type = FunctionType.PAGE, url = "/yddemo/indicatorPackages/page/edit")
	ResponseEntity<IndicatorPackage> update(@PathVariable String id, @RequestBody IndicatorPackage group,
			BindingResult result) {
		IndicatorPackage old = indicatorPackageService.findOne(id);
		if (old == null) {
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
		group.setId(old.getId());
		group.setCompany(old.getCompany());

		validate(group, result);
		IndicatorPackage saved = indicatorPackageService.save(group);
		return ResponseEntity.ok(saved);
	}

	/**
	 * 删除指标组
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@RequiresPermissions("yddemo:indicatorPackage:delete")
	@ApiOperation(value = "删除指标包", notes = "根据ID删除指标组")
	@AppFunctionInfo(key = "yddemo:indicatorPackage:delete", name = "删除指标组", groupKey = "yddemo:indicatorPackage", //
			isMenu = false, type = FunctionType.BUTTON, url = "/yddemo/indicatorPackages")
	ResponseEntity<Void> delete(@PathVariable String id) {
		
		// 不删除
		return ResponseEntity.noContent().build();
	}

	
	
}
